<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="标题">
              <a-input v-model:value.trim="where.title" placeholder="请输入标题" allow-clear />
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="id"
          :datasource="url"
          :columns="columns"
          :where="where"
          v-model:selection="selection"
          :scroll="{ x: 'max-content' }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space>
              <a-button type="primary" @click="openEdit()">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>新建</span>
              </a-button>
            </a-space>
          </template>
          <!-- logo -->
          <template #logo="{ record }">
            <a-image v-if="record.logoUrl" :src="record.logoUrl" alt="logo" id="logo" />
            <div v-else>
              <div class="ant-upload-text">--</div>
            </div>
          </template>
          <!-- linkUrl -->
          <template #linkUrl="{ record }">
            <a :href="record.linkUrl">{{ record.linkUrl }}</a>
          </template>
          <!-- 1是启用，2是禁用 -->
          <template #state="{ record }">
            <a-switch
              checked-children="启用"
              un-checked-children="禁用"
              :checked="record.status === 1"
              @change="checked => editState(checked, record)"
            />
          </template>
          <!-- table操作栏按钮 -->
          <template #action="{ record }">
            <a-space>
              <a @click="openEdit(record)">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
  </div>

  <!-- 编辑弹窗 -->
  <cooperationUnit-edit v-model:visible="showEdit" :data="current" @done="reload" />
</template>

<script>
import { PlusOutlined } from '@ant-design/icons-vue';
import { CooperationUnitApi } from '@/api/cooperationUnit/CooperationUnitApi';
import CooperationUnitEdit from './cooperation_unit_edit';

export default {
  name: 'CooperationUnit',
  components: {
    PlusOutlined,
    CooperationUnitEdit
  },
  data() {
    return {
      // 表格数据接口
      url: '/cooperationUnit/page',
      // 表格列配置
      columns: [
        {
          key: 'index',
          width: 48,
          customRender: ({ index }) => this.$refs.table.tableIndex + index
        },
        {
          title: 'logo',
          dataIndex: 'logoUrl',
          width: 60,
          key: 'logoUrl',
          slots: { customRender: 'logo' }
        },
        {
          title: '标题',
          dataIndex: 'title'
        },
        {
          title: '简介',
          dataIndex: 'introduction'
        },
        {
          title: '链接网址',
          dataIndex: 'linkUrl',
          key: 'linkUrl',
          ellipsis: true,
          slots: { customRender: 'linkUrl' }
        },
        {
          title: '排序',
          width: 100,
          dataIndex: 'sort'
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: 100,
          align: 'center',
          slots: { customRender: 'state' }
        },
        {
          title: '操作',
          key: 'action',
          width: 100,
          align: 'center',
          fixed: 'right',
          slots: { customRender: 'action' }
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author jizhongbao
     * @date 2022/01/10 15:08
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author jizhongbao
     * @date 2022/01/10 15:08
     */
    reset() {
      this.where = {};
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author jizhongbao
     * @date 2022/01/10 15:08
     */
    async remove(row) {
      const result = await CooperationUnitApi.delete({ id: row.id });
      this.$message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author jizhongbao
     * @date 2022/01/10 15:08
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    /**
     * 修改状态
     *
     * @author jizhongbao
     * @date 2022/01/10 15:07
     */
    async editState(checked, row) {
      const id = row.id;
      // 状态：1-启用，2-禁用
      const status = checked ? 1 : 2;
      const result = await CooperationUnitApi.changeStatus({ id, status });
      this.$message.success(result.message);
      row.status = status;
    }
  }
};
</script>
<style>
#logo {
  width: 30px;
  height: 30px;
}
</style>